<?php
require 'lib.php';

$username = P('username');
$password = P('password');

if(empty($username) || empty($password)) {
    error('用户名或密码为空!');
}

$redis = redis_conn();
//1.查询数据
$userid = $redis->get('user:username:'.$username.':userid');

if(!$userid) {
    //1.1 用户不存在
    error('用户不存在，请注册后重试!');
}else {
    //1.2 判断用户名密码是否匹配
    $redis_password = $redis->get('user:userid:'.$userid.':password');
    if($redis_password != md5($password)) {
        error('用户名或密码输入有误,请重新输入!');
    }
    //2.验证通过，cookie操作
    setcookie('userid', $userid);
    setcookie('username', $username);
    header('Location: home.php');exit(0);
}

